﻿using PMIS.WebApi.Cache.Interface;
using PMIS.WebApi.Cache.MemoryCache;
using PMIS.WebApi.Cache.RedisCache;
using StackExchange.Redis;

namespace PMIS.WebApi.Cache
{
    public static class CacheExtension
    {
        public static void AddCacheServices(this IServiceCollection services, IConfiguration configuration)
        {
            // Load configuration
            var cacheProvider = configuration["Cache:CacheProvider"];

            if (cacheProvider == "Memory")
            {
                services.AddMemoryCache();
                services.AddSingleton<ICacheService, MemoryCacheService>();
            }
            else if (cacheProvider == "Redis")
            {
                var redisConnectionString = configuration["Cache:RedisConnectionString"];
                services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisConnectionString));
                services.AddSingleton<ICacheService, RedisCacheService>();
            }
            else
            {
               //
            }
        }
    }
}
